package com.longge;

import java.util.HashMap;
import java.util.Map;

public class LemonadeChange {

    public boolean lemonadeChange(int[] bills) {
        Map<Integer, Integer> billCountMap = new HashMap<>();
        for (int bill : bills) {
            if (bill == 5) {
                billCountMap.put(bill, billCountMap.getOrDefault(bill, 0) + 1);
            } else if (bill == 10) {
                Integer fiveCount = billCountMap.getOrDefault(5,0);
                if (fiveCount < 1) return false;
                billCountMap.put(5, fiveCount - 1);
                billCountMap.put(10, billCountMap.getOrDefault(bill, 0) + 1);
            } else if (bill == 20) {
                Integer fiveCount = billCountMap.getOrDefault(5, 0);
                Integer tenCount = billCountMap.getOrDefault(10, 0);
                if (tenCount != 0) {
                    if (fiveCount < 1) return false;
                    billCountMap.put(5, fiveCount - 1);
                    billCountMap.put(10, tenCount - 1);
                    billCountMap.put(20, billCountMap.getOrDefault(bill, 0) + 1);
                } else {
                    if (fiveCount<3) return false;
                    billCountMap.put(5, fiveCount - 3);
                    billCountMap.put(20, billCountMap.getOrDefault(bill, 0) + 1);
                }
            }
        }
        return true;

    }
}
